package com.gemcoder.system.service;
import static com.gemcoder.system.datasource.JdbcTemplateUtil.executeSQL;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import static com.gemcoder.system.service.UserSdkService.sdkOfGetCurrentUserName;
import static com.gemcoder.system.service.UserSdkService.sdkOfGetCurrentUserId;
import com.gemcoder.system.bo.Result;
import static com.gemcoder.system.service.permission.PermissionSdkService.sdkOfgetAllPermissions;
import static com.gemcoder.system.service.permission.PermissionSdkService.sdkOfgetAllMenus;
import java.sql.SQLException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.date.DateUtil;

@Service
public class CompanybusinesstypeService {

    public Result addCompanyBusinessType(Map<String,Object> paramMap){
    // 获取当前登录用户的用户名
    String createUsername = sdkOfGetCurrentUserName();
    // 获取参数
    String businessType = paramMap.get("CompanyBusinessTypevalue1") != null ? String.valueOf(paramMap.get("CompanyBusinessTypevalue1")) : null;
    Integer projectCount = paramMap.get("Numberofitemsvalue1") != null ? Integer.valueOf(String.valueOf(paramMap.get("Numberofitemsvalue1"))) : null;
    Float percentage = paramMap.get("Percentagevalue1") != null ? Float.valueOf(String.valueOf(paramMap.get("Percentagevalue1"))) : null;

    // 构建插入SQL
    StringBuilder sql = new StringBuilder("INSERT INTO company_business_type (business_type, project_count, percentage, create_username, create_time) VALUES (?, ?, ?, ?, NOW())");
    List<Object> params = new ArrayList<>();
    params.add(businessType);
    params.add(projectCount);
    params.add(percentage);
    params.add(createUsername);

    // 执行插入操作
    int insertRows = executeSQL(sql.toString(), params.toArray());

    // 返回结果
    if (insertRows > 0) {
        return new Result(true, "新增成功", null);
    } else {
        return new Result(false, "新增失败", null);
    }
}

public Result getCompanyBusinessTypes(Map<String,Object> paramMap){
    // 获取分页参数
    Integer current = paramMap.get("current") != null ? Integer.valueOf(String.valueOf(paramMap.get("current"))) : 1;
    Integer pageSize = paramMap.get("pageSize") != null ? Integer.valueOf(String.valueOf(paramMap.get("pageSize"))) : 10;

    // 构建查询SQL
    StringBuilder sql = new StringBuilder("SELECT id, business_type, project_count, percentage FROM company_business_type LIMIT ?, ?");
    List<Object> params = new ArrayList<>();
    params.add((current - 1) * pageSize);
    params.add(pageSize);

    // 执行查询操作
    List<Map<String, Object>> data = executeSQL(sql.toString(), params.toArray());

    // 构建统计总数的SQL
    String countSql = "SELECT COUNT(*) AS total FROM company_business_type";
    List<Map<String, Object>> countResult = executeSQL(countSql);

    // 获取总条数
    Integer total = countResult.get(0).get("total") != null ? Integer.valueOf(String.valueOf(countResult.get(0).get("total"))) : 0;

    // 返回结果
    Map<String, Object> result = new HashMap<>();
    result.put("total", total);
    result.put("data", data);
    return new Result(true, "查询成功", result);
}

public Result deleteCompanyBusinessType(Map<String,Object> paramMap){
    // 获取参数
    Integer id = paramMap.get("id") != null ? Integer.valueOf(String.valueOf(paramMap.get("id"))) : null;

    // 构建删除SQL
    StringBuilder sql = new StringBuilder("DELETE FROM company_business_type WHERE id = ?");
    List<Object> params = new ArrayList<>();
    params.add(id);

    // 执行删除操作
    int deleteRows = executeSQL(sql.toString(), params.toArray());

    // 返回结果
    if (deleteRows > 0) {
        return new Result(true, "删除成功", null);
    } else {
        return new Result(false, "删除失败", null);
    }
}

public Result updateCompanyBusinessType(Map<String,Object> paramMap){
    // 获取当前登录用户的用户名
    String updateUsername = sdkOfGetCurrentUserName();
    // 获取参数
    Integer id = paramMap.get("id") != null ? Integer.valueOf(String.valueOf(paramMap.get("id"))) : null;
    String businessType = paramMap.get("CompanyBusinessTypevalue2") != null ? String.valueOf(paramMap.get("CompanyBusinessTypevalue2")) : null;
    Integer projectCount = paramMap.get("Numberofitemsvalue2") != null ? Integer.valueOf(String.valueOf(paramMap.get("Numberofitemsvalue2"))) : null;
    Float percentage = paramMap.get("Percentagevalue2") != null ? Float.valueOf(String.valueOf(paramMap.get("Percentagevalue2"))) : null;

    // 构建更新SQL
    StringBuilder sql = new StringBuilder("UPDATE company_business_type SET business_type = ?, project_count = ?, percentage = ?, update_username = ?, update_time = NOW() WHERE id = ?");
    List<Object> params = new ArrayList<>();
    params.add(businessType);
    params.add(projectCount);
    params.add(percentage);
    params.add(updateUsername);
    params.add(id);

    // 执行更新操作
    int updateRows = executeSQL(sql.toString(), params.toArray());

    // 返回结果
    if (updateRows > 0) {
        return new Result(true, "更新成功", null);
    } else {
        return new Result(false, "更新失败", null);
    }
}

}